/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builders;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import org.balisunrise.annotation.EventType;
import org.balisunrise.annotation.VisualActionTarget;
import org.balisunrise.annotation.util.NameUtil;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.Action;
import org.balisunrise.common.ActionProvider;
import org.balisunrise.common.TransactionActions;
import org.balisunrise.swing.commons.VisualAction;
import org.balisunrise.swing.commons.listeners.TransactionActionsListener;
import org.balisunrise.swing.commons.listeners.TransactionModeListener;
import org.balisunrise.swing.components.panel.TransactionActionPanel;
import org.balisunrise.swing.components.panel.layouts.TransactionActionLayoutBuilder;

/**
 *
 * @author glauco
 */
public class TransactionActionBuilder implements
        org.balisunrise.builder.TransactionActionBuilder{

    private static final String MESSAGE = "Erro ao construir um TransactionAction.\n";


    @Override
    public TransactionActions create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null)
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");

        org.balisunrise.swing.commons.TransactionActions ta =
                new org.balisunrise.swing.commons.TransactionActions();

        ta.setAlias(alias);
        ta.setEntityClass(entityClass);
        ta.setTitle("");

        // cria as ações
        List<org.balisunrise.common.VisualAction> list = Application.get()
                .getVisualEntityManager()
                .getActionBuilder()
                .createTransactionAction(ta);

        // ações
        VisualAction searchAction = (VisualAction) list.get(0);
        VisualAction newAction = (VisualAction) list.get(1);
        VisualAction editAction = (VisualAction) list.get(2);
        VisualAction deleteAction = (VisualAction) list.get(3);

        // Cria Ações Visuais da transaction
        ActionProvider ap = Application.get()
                .getActionMapManager()
                .getActionProvider(entityClass);
        org.balisunrise.common.VisualAction[] vas = ap.getVisualActions(
                VisualActionTarget.TRANSACTION, alias, null);
        // adiciona as ações visuais ao painel de ações
        for (org.balisunrise.common.VisualAction va : vas) {
            va.setOwner(ta);
            ta.addAction(va);
        }

        // Listener de ações visuais
        TransactionActionsListener tal = new TransactionActionsListener();
        ta.addTransactionActionsListener(tal);
        
        // ações de eventos visuais da transaction action
        Action[] as = ap.getActions(EventType.VISUAL_TRANSACTION,
                alias, null);
        for (Action action : as) {
            action.setOwner(ta);
            tal.addVisualAction(action);
        }
        
        // listener de alteração do modo de uma transaction
        TransactionModeListener tml = new TransactionModeListener();
        ta.addModeChangedListener(tml);
        
        as = ap.getActions(EventType.TRANSACTION_MODE_CHANGED, alias, null);
        for (Action action : as) {
            action.setOwner(ta);
            tml.addTransactionModeChangedAction(action);
        }
        
        // lista de botões de outras ações
        List<JButton> actionsButtons = new ArrayList<JButton>();

        // cria RV
        TransactionActionPanel vr = new TransactionActionPanel(
                searchAction.getVisualRepresentation(),
                newAction.getVisualRepresentation(),
                editAction.getVisualRepresentation(),
                deleteAction.getVisualRepresentation(),
                actionsButtons,
                new TransactionActionLayoutBuilder());

        // atribui RV
        ta.setVisualRepresentation(vr);

        // atribui nome
        String name = NameUtil.buildName(ta);
        ta.setName(name);
        vr.setName(name);
        vr.getAccessibleContext().setAccessibleName(name);

        return ta;
    }
}
